﻿using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RevitAddin1
{
    public class BeeFaceFailureHandler : IFailuresPreprocessor
    {
        public string ErrorMessage { set; get; }
        public string ErrorSeverity { set; get; }

        public BeeFaceFailureHandler()
        {
            ErrorMessage = "";
            ErrorSeverity = "";
        }

        public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
        {
            IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
            foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
            {
                FailureDefinitionId id = failureMessageAccessor.GetFailureDefinitionId();
                try
                {
                    ErrorMessage = failureMessageAccessor.GetDescriptionText();
                }
                catch
                {
                    ErrorMessage = "Unknown Error";
                }
                try
                {
                    FailureSeverity failureSeverity = failureMessageAccessor.GetSeverity();
                    ErrorSeverity = failureSeverity.ToString();
                    if (failureSeverity == FailureSeverity.Warning)
                    {
                        //if (failureMessageAccessor.GetDescriptionText() .Contains ("同一位置处具有相同实例"))
                        //{
                        //    List<ElementId> c = failureMessageAccessor.GetFailingElementIds().ToList();
                        //    if (c.Count > 1)
                        //    {
                        //        failuresAccessor.DeleteElements(new List<ElementId>() { c[1] });
                        //    }
                        //}
                        //else
                        //{
                        // 如果是警告，则禁止消息框  
                        failureMessageAccessor.GetDefaultResolutionCaption();
                        //}
                        failuresAccessor.DeleteWarning(failureMessageAccessor);
                    }
                    else
                    {
                        // 如果是错误：则取消导致错误的操作，但是仍然继续整个事务  
                        if (ErrorMessage.Contains("线太短"))
                        {
                            failureMessageAccessor.SetCurrentResolutionType(FailureResolutionType.DeleteElements);
                            failuresAccessor.ResolveFailure(failureMessageAccessor);
                            return FailureProcessingResult.ProceedWithCommit;
                        }
                        //不能满足定义高亮显示的图元的绘制约束。
                        if (ErrorMessage.Contains("不能满足定义高亮显示的图元的绘制约束。"))
                        {
                            //failureMessageAccessor.SetCurrentResolutionType(FailureResolutionType.Default);
                            //failuresAccessor.ResolveFailure(failureMessageAccessor);


                            return FailureProcessingResult.ProceedWithCommit;
                        }
                        //else
                        //{
                        //    failuresAccessor.DeleteWarning(failureMessageAccessor);
                        //    return FailureProcessingResult.ProceedWithCommit;
                        //}
                    }
                }
                catch (Exception)
                {
                }
            }
            return FailureProcessingResult.Continue;
        }


        /// <summary>
        /// 这个方法用在事务开始前，在FailureHandler初始化后调用
        /// </summary>
        public static void SetFailedHandlerBeforeTransaction(IFailuresPreprocessor failureHandler, Transaction transaction)
        {
            FailureHandlingOptions failureHandlingOptions = transaction.GetFailureHandlingOptions();
            failureHandlingOptions.SetFailuresPreprocessor(failureHandler);
            // 这句话是关键  
            //failureHandlingOptions.SetClearAfterRollback(true);
            transaction.SetFailureHandlingOptions(failureHandlingOptions);
        }
    }
}
